<template>
  <div>
    <el-card>
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input placeholder="手机号" size="small" class="input-with-select" v-model="queryInfo.mobile" clearable
                    @clear="getDataList"/>
        </el-col>
        <el-col :span="3">
          <el-button type="primary" size="small" style="margin-left: 10px;" @click="getDataList">搜索</el-button>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" size="small" @click="showEditDialog('')">添加</el-button>
        </el-col>
      </el-row>
      <el-table :data="dataList" border stripe>
        <el-table-column label="绑定手机" prop="mobile" width="130"></el-table-column>
        <el-table-column label="批次ID" prop="batchId" width="80"></el-table-column>
        <el-table-column label="批次名称" prop="batchName"></el-table-column>
        <el-table-column label="绑定卡号" prop="couponNos"></el-table-column>
        <el-table-column label="绑定时间" prop="addTime" width="150"></el-table-column>
        <el-table-column label="绑定理由" prop="reason"></el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                     :current-page="queryInfo.pageNo" :page-sizes="[5, 10, 20]"
                     :page-size="queryInfo.pageSize" layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="管理员绑定卡券" :visible.sync="editDialogVisible" width="40%" @close="editDialogClosed">
      <el-form :model="editForm" label-width="180px" class="demo-ruleForm">
        <el-form-item label="绑定手机号" prop="mobile">
          <el-input v-model="editForm.mobile" style="width: 140px"></el-input>
        </el-form-item>
        <el-form-item label="管理批次" prop="batchId">
          <el-select v-model="editForm.batchId" placeholder="请选择批次">
            <el-option
                v-for="item in couponBatchList"
                :key="Number(item.id)"
                :label="item.name"
                :value="Number(item.id)">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="绑卡数量" prop="quantity">
          <el-input-number v-model="editForm.quantity" :min="1" :max="10"></el-input-number>
        </el-form-item>
        <el-form-item label="绑定理由" prop="reason">
          <el-input type="textarea" v-model="editForm.reason"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveUserBindCoupon()">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import Api from "@/api/Api.js";

export default {
  data() {
    return {
      drawer: false,
      dialogVisible: false,
      disabled: false,
      queryInfo: {
        mobile: '',
        pageNo: 1,
        pageSize: 20, // 页大小
      },
      editDialogVisible: false,
      editForm: {
        quantity: 1
      },
      dataList: [],
      couponBatchList: [],
      total: 0
    };
  },
  watch: {},
  created() {
    this.getCouponBatchList();
    this.getDataList();
  },
  methods: {
    getDataList() {
      Api.getUserBindCouponList(this.queryInfo).then((res) => {
        if (res.success) {
          this.dataList = res.data.records;
          this.total = res.data.total;
        } else {
          return this.$message.error("获取列表失败");
        }
      });
    },
    getCouponBatchList() {
      Api.getCouponBatchList({'pageSize': 100}).then((res) => {
        if (res.success) {
          this.couponBatchList = res.data.records;
        } else {
          return this.$message.error("获取列表失败");
        }
      });
    },
    async showEditDialog(id) {
      this.editDialogVisible = true
    },
    editDialogClosed() {

    },
    saveUserBindCoupon() {
      Api.saveUserBindCoupon(this.editForm).then((res) => {
        if (res.success) {
          this.$message.success("保存成功");
          this.editDialogVisible = false;
          this.getDataList();
        } else {
          return this.$message.error("保存失败：" + res.msg);
        }
      });
    },
    handleSizeChange(pageSize) {
      this.queryInfo.pageSize = pageSize;
      this.getDataList();
    },
    handleCurrentChange(pageNo) {
      this.queryInfo.pageNo = pageNo;
      this.getDataList();
    },
  }
};
</script>

<style lang="less" scoped>
.logo_pic {
  max-width: 100px;
}

.upload_img {
  width: 148px;
  height: 148px;
  object-fit: cover;
  border-radius: 6px;
  margin-right: 20px;
}
</style>
